import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { ActivatedRoute, Params } from '@angular/router'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { MineService } from '../index/services/mine.service'
import { CoolSessionStorage } from 'src/app/common/coolStorage/cool-session-storage'

@Component({
  selector: 'app-person-modify',
  templateUrl: './person-modify.component.html',
  styleUrls: ['./person-modify.component.scss']
})
export class PersonModifyComponent implements OnInit {
  // 编辑页面参数
  item = {
    type: '',
    title: '个人资料',
    placeholder: ''
  }
  // 提交参数
  formdata = {
    birthday: null,
    certificateId: null,
    certificateType: null,
    name: null,
    nick: null,
    photo: null,
    sex: null
  }
  // 提交按钮启用禁用
  disabled = false

  constructor(
    private location: Location,
    private activatedRoute: ActivatedRoute,
    private utils: CommonUtil,
    private service: MineService,
    private session: CoolSessionStorage
  ) {
    activatedRoute.params.subscribe((params: Params) => {
      this.item.type = params.type
      if (params.type === 'Nick') {
        this.item.title = '修改昵称'
        this.item.placeholder = '请输入昵称'
        this.formdata.nick = params.value
      } else if (params.type === 'Sex') {
        this.item.title = '选择性别'
        this.item.placeholder = '请选择性别'
        this.formdata.sex = params.value || '男'
      }
    })
  }

  // 初始化
  ngOnInit() {}
  // 返回上一页
  onClickLeft() {
    this.location.back()
  }
  // 提交表单
  submit() {
    this.utils.loading('正在提交...')
    this.disabled = true
    this.service.modifyCurrentUser(
      this.formdata,
      res => {
        this.utils.hide()
        this.disabled = false
        if (res.Success && res.Result) {
          this.session.setItem('personModify', 'true')
          this.utils.success('操作成功')
          this.location.back()
        }
      },
      () => {
        this.utils.hide()
        this.disabled = false
      }
    )
  }
}
